class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
        int k = indexDiff;
        int t = valueDiff;
        set<int> st;
        int n = nums.size();
        for(int i = 0;i < n;i++){
            auto p = st.lower_bound((long)(nums[i] - t));
            if(p != st.end() && (long)abs(nums[i] - *p) <= t){
                return true;
            }
            st.insert(nums[i]);
            if(i >= k){
                st.erase(nums[i-k]);
            }
        }
        return false;
    }
};
